package com.example.a10928.weifriend.data;

import android.text.TextUtils;


import com.example.a10928.weifriend.AppScope;
import com.example.a10928.weifriend.data.pref.DefaultPrefManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.Expose;

import javax.inject.Inject;

/**
 * Created by Chuang on 2018/3/2.
 * 用户信息管理
 */

@AppScope
public class User {

    @Expose
    private String userId;
    @Expose
    private String nickName;
    @Expose
    private String phoneNo;
    @Expose
    private String token;
    @Expose
    private String mail;
    @Expose
    private String headImg;
    @Expose
    private String password;
    @Expose
    private String account;
    @Expose
    private String realName;
    @Expose
    private String bankName;
    @Expose
    private String bankNum;
    @Expose
    private int bankId;

    private DefaultPrefManager mPref;
    private Gson mGson;

    @Inject
    User(DefaultPrefManager defaultPrefManager) {
        mPref = defaultPrefManager;
        mGson = new GsonBuilder()
            .excludeFieldsWithoutExposeAnnotation()
            .create();
        readFromDisk();
    }

    private void readFromDisk() {
        String userJson = mPref.getUserJson();
        if (TextUtils.isEmpty(userJson)) {
            resetToDefault();
        } else {
            User user = mGson.fromJson(userJson, User.class);
            userId = user.userId;
            nickName = user.nickName;
            phoneNo = user.phoneNo;
            token = user.token;
            mail = user.mail;
            headImg = user.headImg;

            realName=user.realName;
            bankId=user.bankId;
            bankName=user.bankName;
            bankNum=user.bankNum;
        }
    }

    public void saveUser(String account, String password, String userId, String nickName, String phoneNo, String token, String mail, String headImg) {
        this.userId = userId;
        this.nickName = nickName;
        this.phoneNo = phoneNo;
        this.password = password;
        this.account = account;
        this.token = "Bearer " + token;
        this.headImg = headImg;
        this.mail = mail;
        saveToDisk();
    }

    public void saveHeadImg(String img) {
        this.headImg = img;
        saveToDisk();
    }

    public void saveAccount(String account) {
        this.account = account;
        saveToDisk();
    }

    public void savePasssword(String password) {
        this.password = password;
        saveToDisk();
    }


    public boolean isLogin() {
        return !TextUtils.isEmpty(token) && !TextUtils.isEmpty(phoneNo);
    }

    private void saveToDisk() {
        mPref.setUserJson(mGson.toJson(this));
    }





    public void clearUser() {
        saveToDisk();
        resetToDefault();
    }

    private void resetToDefault() {
        userId = "";
        nickName = "";
        phoneNo = "";
        token = "";
        mail = "";
        headImg = "";

    }

    public String getUserId() {
        return userId;
    }



    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPhoneNo() {
        return phoneNo;
    }

    public String getToken() {
        return token;
    }

    public String getMail() {
        return mail;
    }

    public String getHeadImg() {
        return headImg;
    }

    public String getPassword() {
        return password;
    }

    public String getAccount() {
        return account;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
        saveToDisk();
    }

    public String getBankName() {
        return bankName;
    }

    public void setBankName(String bankName) {
        this.bankName = bankName;
        saveToDisk();
    }

    public String getBankNum() {
        return bankNum;
    }

    public void setBankNum(String bankNum) {
        this.bankNum = bankNum;
        saveToDisk();
    }

    public int getBankId() {
        return bankId;
    }

    public void setBankId(int id) {
        this.bankId = id;
        saveToDisk();
    }
}
